package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.alibaba.fastjson.JSONArray;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zero
 * @since 2021/12/2 17:09
 */
@Component
public class ExportRiskDistrictResponsibleUser implements FieldValueConvert<String, String> {
    
    @Autowired
    private BaseUserDao baseUserDao;

    @Override
    public String convert(Object bean, String value, DataDumpField dataField) {
        if (StringUtils.isNotBlank(value)){
            List<Long> userList = JSONArray.parseArray(value, Long.class);
            if (!CollectionUtils.isEmpty(userList)) {
                List<String> resultList = new ArrayList<>();
                for (Long id : userList) {
                    String name = baseUserDao.getRealNameById(id);
                    resultList.add(name);
                }
                return StringUtils.join(resultList.toArray(),",");
            }
        }
        return null;
    }
}
